import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header


def connectServer():
    connect = smtplib.SMTP_SSL('smtp.qq.com', 465)
    connect.login("738897958@qq.com", 'lsagqrmxcvhrbajh')
    return connect


def sendEmail(sender, reciver, subject, content):
    connect = connectServer()
    msg = MIMEMultipart()
    msg['Subject'] = subject
    # 设置邮件发送者
    msg['From'] = sender
    # 设置邮件接受者
    msg['To'] = reciver
    # 添加⽂文字内容
    print(content)
    if isinstance(content, list):
        for li in content:
            msg.attach(li)
    else:
        msg.attach(content)

    try:
        connect.sendmail(sender, reciver, msg.as_string())
        connect.quit()
        print("邮件发送成功")
    except smtplib.SMTPException as e:
        print('邮件发送失败')
        print(e)


if __name__ == '__main__':
    sender = "738897958@qq.com"
    reciver = "228985150@qq.com"
    subject = Header('找回密码', 'utf-8').encode()
    text = MIMEText("我给你发送了几个文件，请阅读", "plain", 'utf-8')
    file1 = MIMEText(open("C:/Users/e-Xingfu.Yang/Desktop/Tmp/access.log", "rb").read(), "base64", "utf-8")
    file1["Content-Disposition"] = "attachment; filename='access.log'"
    file2 = MIMEText(open("C:/Users/e-Xingfu.Yang/Desktop/Tmp/output.log", "rb").read(), "base64", "utf-8")
    file2["Content-Disposition"] = "attachment; filename='output.log'"
    content = [text, file1, file2]
    sendEmail(sender, reciver, subject, content)
